<template>
  <view>
    <!-- 预留出的搜索区域 -->
    <MySearch @click.native="goSearch"></MySearch>
    <view class="scroll-view-container">
      <!-- 左侧的滚动区域 -->
      <!-- 左侧的滚动区域 -->
      <scroll-view
        class="left-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <block v-for="(item1, index) in categoryList" :key="index">
          <view
            class="left-scroll-view-item"
            @click="selectedCategory(index)"
            :class="{ active: index === categoryActive }"
            >{{ item1.cat_name }}</view
          >
        </block>
      </scroll-view>

      <!-- 右侧的滚动区域 -->
      <scroll-view
        class="right-scroll-view"
        :scroll-top="scrollTop"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <!-- 动态渲染二级分类的列表数据 -->
        <view
          class="cate-lv2"
          v-for="(item2, index2) in categoryList[categoryActive].children"
          :key="index2"
        >
          <!-- 二级分类的标题 -->
          <view class="cate-lv2-title">/ {{ item2.cat_name }} /</view>
          <!-- 动态渲染三级分类的列表数据 -->
          <view class="cate-lv3-list">
            <view
              class="cate-lv3-item"
              v-for="(item3, index3) in item2.children"
              :key="index3"
              @click="gotoProductList(item3)"
            >
              <image :src="item3.cat_icon"></image>
              <!-- 三级分类的种类名字 -->
              <text>{{ item3.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import MySearch from '@/components/my-search'
import badge from '@/mixins/badge.js'
export default {
  mixins: [badge], 
  components: {
    MySearch
  },
  data() {
    return {
      wh: 0,
      categoryActive: 0, // 高亮的下标 默认是第一项
      categoryList: [],
      scrollTop: 0 // 滚动条的位置
    }
  },

  created() {
    // 动态获取窗口可用高度，以便让页面中的左右两块滚动区域的高度撑满屏幕
    // 1. 获取当前系统信息
    const sysInfo = uni.getSystemInfoSync()

    // 2. 从系统信息中获取窗口可用高度并赋值给 wh
    // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar高度 
    this.wh = sysInfo.windowHeight - 50 // 额外在减去搜索高度
    // ----------------------------------
    // 获取分类数据
    this.getCategoryList()
  },
  methods: {
    goSearch() {
      uni.navigateTo({
        url: '/pages_sub1/search/search'
      })
    },
    // 选中高亮
    selectedCategory(index) {
      // 滚动条回到顶部
      // 0 ---> 1   组件更新
      // 100 ---> 0 组件更新
      this.scrollTop = this.scrollTop === 0 ? 1 : 0
      this.categoryActive = index
    },
    gotoProductList(item) {
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?cid=' + item.cat_id
      })
    },
    // 获取分类数据
    async getCategoryList() {
      const {
        message,
        meta: { status },
      } = await this.$http.get("/categories")
      if (status !== 200) return this.$showMsg()
      this.categoryList = message
    },
  },
}
</script>

<style scoped lang="scss">
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 120rpx;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 24rpx;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: " ";
          display: block;
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
  .cate-lv3-list {
    display: flex;
    flex-wrap: wrap;

    .cate-lv3-item {
      width: 33.33%;
      margin-bottom: 20rpx;
      display: flex;
      flex-direction: column;
      align-items: center;

      image {
        width: 120rpx;
        height: 120rpx;
      }

      text {
        font-size: 24rpx;
      }
    }
  }
  .cate-lv2-title {
    font-size: 24rpx;
    font-weight: bold;
    text-align: center;
    padding: 30rpx 0;
  }
}
</style>